package com.peanut.baby.mvp.sign;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.DailySign;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/24.
 */

public class SignPresenter implements SignContract.Presenter{
    private SignContract.View view;
    Activity activity;
    public SignPresenter(SignContract.View view,Activity activity) {
        this.view = view;
        this.activity  =activity;
    }

    @Override
    public void requestData() {
        Observable obs = RetrofitClient.getInstance().dailySign(InitManager.getInstance().getUserId(),
                    InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<DailySign>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<DailySign>() {
            @Override
            protected void onHandleSuccess(DailySign o) {
                InitManager.getInstance().getUser().userPoint = o.totalPoint;
                view.onDataGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(msg);
            }
        });
    }
}
